import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

// 解决路由跳转报错
// const originalPush = Router.prototype.push
// Router.prototype.push = function push(location) {
//     return originalPush.call(this, location).catch(err => err)
// }

let router =  new Router({
    // mode: 'history',
    routes: [{
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: () => import( /* webpackChunkName: "login" */ '../pages/Login.vue'),
            // component: login,
            meta: {
                title: '登录'
            }
        },
        {
            path: '/register',
            component: () => import( /* webpackChunkName: "login" */ '../pages/Register.vue'),
            // component: register,
            meta: {
                title: '注册'
            }
        },
        {
            path: '/password_reset',
            component: () => import( /* webpackChunkName: "login" */ '../pages/ForgetPassword.vue'),
            // component: password_reset,
            meta: {
                title: '忘记密码'
            }
        },
        {
            path: '/home',
            component: () => import( /* webpackChunkName: "home" */ '../pages/Homepage.vue'),
            // component: homepage,
            meta: {
                title: '首页'
            }
        },
        {
            path: '/userinfo',
            component: () => import( /* webpackChunkName: "userinfo" */ '../pages/UserInfo.vue'),
            // component: userinfo,
            meta: {
                title: '用户信息'
            }
        },
        {
            path: '/systemsetting',
            component: () => import( /* webpackChunkName: "systemsetting" */ '../pages/SystemSetting.vue'),
            // component: systemsetting,
            meta: {
                title: '系统设置'
            }
        },
        {
            path: '/global_deterministic',
            component: () => import( /* webpackChunkName: "dashboard" */ '../pages/Dashboard.vue'),
            redirect: '/global_deterministic/heightfield',
            meta: {
                title: '系统首页'
            },
            children: [{
                    path: 'heightfield',
                    component: () => import( /* webpackChunkName: "Deterministic forecast" */ '../pages/Deterministic forecast/Mainpage.vue'),
                    meta: {
                        title: '全球确定性预报-格点高度场'
                    }
                },
                {
                    path: 'temperaturefield',
                    component: () => import( /* webpackChunkName: "Deterministic forecast" */ '../pages/Deterministic forecast/Mainpage.vue'),
                    meta: {
                        title: '全球确定性预报-格点温度场'
                    }
                },
                {
                    path: 'windfield',
                    component: () => import( /* webpackChunkName: "Deterministic forecast" */ '../pages/Deterministic forecast/Mainpage.vue'),
                    meta: {
                        title: '全球确定性预报-格点风场'
                    }
                },
                {
                    path: 'buferheightfield',
                    component: () => import( /* webpackChunkName: "Deterministic forecast" */ '../pages/Deterministic forecast/Mainpage.vue'),
                    meta: {
                        title: '全球确定性预报-BUFFER高度场'
                    }
                },
                {
                    path: 'bufertemperaturefield',
                    component: () => import( /* webpackChunkName: "Deterministic forecast" */ '../pages/Deterministic forecast/Mainpage.vue'),
                    meta: {
                        title: '全球确定性预报-BUFFER温度场'
                    }
                },
                {
                    path: 'buferwindfield',
                    component: () => import( /* webpackChunkName: "Deterministic forecast" */ '../pages/Deterministic forecast/Mainpage.vue'),
                    meta: {
                        title: '全球确定性预报-BUFFER风场'
                    }
                },
                {
                    path: 'scorecard',
                    component: () => import( /* webpackChunkName: "Deterministic forecast" */ '../pages/Deterministic forecast/ScoreCard.vue'),
                    meta: {
                        title: '全球确定性预报-综合评分卡'
                    }
                },
                {
                    path: 'modulesetting',
                    component: () => import( /* webpackChunkName: "Atmospheric diffusion" */ '../pages/ModuleSetting.vue'),
                    meta: {
                        title: '全球确定性预报-模块设置'
                    }
                },
            ]
        },
        {
            path: '/grapes_reps',
            component: () => import( /* webpackChunkName: "dashboard" */ '../pages/Dashboard.vue'),
            // component: EFdashboard,
            redirect: '/grapes_reps/aroc',
            meta: {
                title: '系统首页'
            },
            children: [{
                    path: 'aroc',
                    meta: {
                        title: '集合预报-AROC',
                        page: 'AROC',
                    },
                    component: () => import( /* webpackChunkName: "Ensemble forecast" */ '../pages/Ensemble forecast/Aroc.vue'),
                },
                {
                    path: 'bs',
                    meta: {
                        title: '集合预报-BS',
                        page: 'BS',
                    },
                    component: () => import( /* webpackChunkName: "Ensemble forecast" */ '../pages/Ensemble forecast/Bs.vue'),
                },
                {
                    path: 'modulesetting',
                    meta: {
                        title: '集合预报-模块设置'
                    },
                    component: () => import( /* webpackChunkName: "Atmospheric diffusion" */ '../pages/ModuleSetting.vue'),
                }
            ]
        },
        {
            path: '/ADdashboard',
            component: () => import( /* webpackChunkName: "dashboard" */ '../pages/Dashboard.vue'),
            redirect: '/ADdashboard/windfield',
            meta: {
                title: '系统首页'
            },
            children: [{
                    path: 'windfield',
                    component: () => import( /* webpackChunkName: "Atmospheric diffusion" */ '../pages/Atmospheric diffusion/WindField.vue'),
                    // component: windfield,
                    meta: {
                        title: '大气扩散模块-风场图'
                    }
                },
                {
                    path: 'concentration',
                    component: () => import( /* webpackChunkName: "Atmospheric diffusion" */ '../pages/Atmospheric diffusion/Concentration.vue'),
                    meta: {
                        title: '大气扩散模块-浓度图'
                    }
                },
                {
                    path: 'windfield-concentration',
                    component: () => import( /* webpackChunkName: "Atmospheric diffusion" */ '../pages/Atmospheric diffusion/WindField-Concentration.vue'),
                    meta: {
                        title: '大气扩散模块-风场浓度图'
                    }
                },
                {
                    path: 'modulesetting',
                    component: () => import( /* webpackChunkName: "Atmospheric diffusion" */ '../pages/ModuleSetting.vue'),
                    meta: {
                        title: '大气扩散模块-模块设置'
                    }
                }
            ]
        },
        {
            path: '/grapes_region_determ',
            component: () => import( /* webpackChunkName: "dashboard" */ '../pages/Dashboard.vue'),
            redirect: '/grapes_region_determ/heightfield',
            meta: {
                title: '模块首页'
            },
            children: [
                {
                    path: 'heightfield',
                    component: () => import( /* webpackChunkName: "grapes_region_determ" */ '../pages/grapes_region_determ/Mainpage.vue'),
                    meta: {
                        title: 'GRAPES高分辨率确定性预报-格点高度场'
                    }
                }, {
                    path: 'temperaturefield',
                    component: () => import( /* webpackChunkName: "grapes_region_determ" */ '../pages/grapes_region_determ/Mainpage.vue'),
                    meta: {
                        title: 'GRAPES高分辨率确定性预报-格点温度场'
                    }
                }, {
                    path: 'windfield',
                    component: () => import( /* webpackChunkName: "grapes_region_determ" */ '../pages/grapes_region_determ/Mainpage.vue'),
                    meta: {
                        title: 'GRAPES高分辨率确定性预报-格点风场'
                    }
                }, {
                    path: 'buferheightfield',
                    component: () => import( /* webpackChunkName: "grapes_region_determ" */ '../pages/grapes_region_determ/Mainpage.vue'),
                    meta: {
                        title: 'GRAPES高分辨率确定性预报-BUFFER高度场'
                    }
                }, {
                    path: 'bufertemperaturefield',
                    component: () => import( /* webpackChunkName: "grapes_region_determ" */ '../pages/grapes_region_determ/Mainpage.vue'),
                    meta: {
                        title: 'GRAPES高分辨率确定性预报-BUFFER温度场'
                    }
                }, {
                    path: 'buferwindfield',
                    component: () => import( /* webpackChunkName: "grapes_region_determ" */ '../pages/grapes_region_determ/Mainpage.vue'),
                    meta: {
                        title: 'GRAPES高分辨率确定性预报-BUFFER风场'
                    }
                }, {
                    path: 'scorecard',
                    component: () => import( /* webpackChunkName: "grapes_region_determ" */ '../pages/grapes_region_determ/ScoreCard.vue'),
                    meta: {
                        title: 'GRAPES高分辨率确定性预报-综合评分卡'
                    }
                }, {
                    path: 'modulesetting',
                    component: () => import( /* webpackChunkName: "Atmospheric diffusion" */ '../pages/ModuleSetting.vue'),
                    meta: {
                        title: 'GRAPES高分辨率确定性预报-模块设置'
                    }
                },
            ]
        },
        {
            path: '/404',
            component: () => import(/* webpackChunkName: "404" */ '../pages/404.vue'),
            // component: notfound,
            meta: {
                title: '404'
            }
        },
        {
            path: '*',
            redirect: '/404'
        },
        {
            path: '/charttest',
            component: () => import(/* webpackChunkName: "charttest" */ '../components/common/Charts.vue'),
            meta: {
                title: 'echart图表测试'
            }
        }
    ]
});

//使用钩子函数在路由跳转前处理操作
router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | GRAPES`; // 改变页面标题名称
    
    const noLoginPaths = ['login', 'register', 'password_reset'];
    // 对未登录用户设置全局路由拦截
    if (!noLoginPaths.some(p => {return to.path.indexOf(p) !== -1})){
        const user = localStorage.getItem('username');
        if (!user) {
            next('/login');
            Vue.prototype.$message.warning('您还未登录，请先登录');
        }
    }

    const authority = localStorage.getItem('authority')
    const authority_A = ['DFdashboard', 'EFdashboard']
    const authority_B = ['ADdashboard']
    if (authority_A.some(p => {return to.path.indexOf(p) !== -1})) {
        if (authority != 'A') {
            next('/home');
            Vue.prototype.$message.warning('您没有权限进入该模块');
        }
    }
    else if (authority_B.some(p => {return to.path.indexOf(p) !== -1})) {
        if (authority != 'B') {
            next('/home');
            Vue.prototype.$message.warning('您没有权限进入该模块');
        }
    }
    next();
});

export default router